//
//  SegmentedControl.swift
//  gamlify
//
//  Created by Admin on 2024/12/8.
//

import UIKit

class SegmentedControl: UIView {
    // MARK: - Properties
    private var buttons: [UIButton] = []
    private var labels: [UILabel] = []
    private var selectorView: UIView!
    
    var textColor: UIColor = UIColor.hex(0xA7B6CC)
    var selectorTextColor: UIColor = .white
    var selectorColor: UIColor = UIColor.hex(0x5050FA)
    
    // callback
    var valueChanged: ((Int) -> Void)?
    
    var selectedIndex: Int = 0 {
        didSet {
            updateView()
        }
    }
    
    var items: [String] = [] {
        didSet {
            setupButtons()
        }
    }
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    
    // MARK: - Setup
    private func setupView() {
        backgroundColor = .clear
        layer.masksToBounds = true
        // 设置border
        layer.borderWidth = 1
        layer.borderColor = UIColor.hex(0xC1D0E5).cgColor
        
        backgroundColor = UIColor.hex(0xE9EFF7)
    }
    
    private func setupButtons() {
        buttons.forEach { $0.removeFromSuperview() }
        buttons = []
        
        selectorView = UIView(frame: .zero)
        selectorView.backgroundColor = selectorColor
        selectorView.layer.cornerRadius = 4
        selectorView.layer.masksToBounds = true
        addSubview(selectorView)
        
        for (index, title) in items.enumerated() {
            let label = UILabel()
            label.text = title
            label.textColor = textColor
            label.font = .systemFont(ofSize: 14)
            label.textAlignment = .center
            addSubview(label)
            labels.append(label)
            
            let button = UIButton(type: .system)
            button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
            button.tag = index
            addSubview(button)
            buttons.append(button)
        }
    }
    
    // MARK: - Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layer.cornerRadius = 6
        
        let buttonWidth = frame.width / CGFloat(buttons.count)
        let buttonHeight = frame.height
        
        for (index, button) in buttons.enumerated() {
            button.frame = CGRect(x: buttonWidth * CGFloat(index), y: 0, width: buttonWidth, height: buttonHeight)
            labels[index].frame = button.frame
        }
        
        let selectorWidth = buttonWidth - 4
        let selectorHeight = frame.height - 4
        selectorView.frame = CGRect(x: 2 + CGFloat(selectedIndex) * buttonWidth, y: 2, width: selectorWidth, height: selectorHeight)
    }
    
    // MARK: - Actions
    @objc private func buttonTapped(_ sender: UIButton) {
        selectedIndex = sender.tag
        valueChanged?(selectedIndex)
    }
    
    // MARK: - Update View
    private func updateView() {
        UIView.animate(withDuration: 0.3) {
            self.selectorView.frame.origin.x = (self.frame.width / CGFloat(self.buttons.count)) * CGFloat(self.selectedIndex) + 2
        }
        
        for (index, label) in self.labels.enumerated() {
            label.textColor = index == self.selectedIndex ? self.selectorTextColor : self.textColor
        }
    }
}
